package yl.hs.bmipfull.daos.finance;

import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import yl.hs.bmipfull.daos.BaseDao;
import yl.hs.bmipfull.pojo.finance.ContractAmount;
import yl.hs.bmipfull.pojo.finance.ShipSettleMainEntity;
import yl.hs.bmipfull.pojo.finance.ShipSettleTemp;
import yl.hs.bmipfull.pojo.production.ProductShipOrderEntity;
import yl.hs.bmipfull.pojo.salemanage.*;
import yl.hs.bmipfull.viewmodels.report.ShipSettleReportVO;
import yl.hs.bmipfull.viewmodels.salemanmage.SetTimeVO;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * IShipSettleMainDao 发货结算单数据操作接口
 * @category 财务管理-发货结算单
 * @author 谢景广
 * @date 2021-12-29
 */
@Repository
public interface IShipSettleMainDao extends BaseDao<ShipSettleMainEntity> {

    Integer queryCountByContractSN(@Param("map") Map<String, Object> map, @Param("filters") List<Map<String, Object>> filters,@Param("snList")List<String> snList);
    Integer queryCountByCondition(@Param("map") Map<String, Object> map, @Param("filters") List<Map<String, Object>> filters);
    List<ShipSettleMainEntity> queryPageContractSN(@Param("map") Map<String, Object> map, @Param("filters") List<Map<String, Object>> filters,@Param("snList")List<String> snList);
    List<ShipSettleMainEntity> queryPage(@Param("map")Map<String, Object> query,@Param("filters")  List<Map<String, Object>> filters);

    List<ShipSettleMainEntity> queryTempSettleList(@Param("map")Map<String, Object> query,@Param("calcField")String calcField);

    /**
     *
     * @param settleSN
     * @return
     */
    Integer deleteBySettleSN(@Param("settleSN") String settleSN);

    /**
     *
     * @param query
     * @return
     */
    List<ShipSettleTemp> queryTempSettleOrders(@Param("map")Map<String, Object> query, @Param("filters") List<Map<String,Object>> filters);

    List<SaleContractEntity> querySettleContract(@Param("map")Map<String, Object> query,@Param("filters") List<Map<String,Object>> filters);
    List<SaleContractProductsEntity> querySettleContractDetails(@Param("map")Map<String, Object> query,@Param("filters") List<Map<String,Object>> filters);
    List<ContractAdditionalFeeEntity> querySettleContractAddition(@Param("map")Map<String, Object> query,@Param("filters") List<Map<String,Object>> filters);
    List<ContractPriceAdjustmentEntity> querySettleContractAdjust(@Param("map")Map<String, Object> query,@Param("filters") List<Map<String,Object>> filters);

    List<ProductPricesEntity> querySettleProductPrice(@Param("map")Map<String, Object> query,@Param("filters") List<Map<String,Object>> filters);

    List<ShipSettleMainEntity> queryContractSettleLast(@Param("map")Map<String, Object> query,@Param("filters") List<Map<String,Object>> filters);

    Integer changeOrderToUnSettle(String settleSN);

    Integer changeOrderToSettled(String settleSN);


    /**
     * 根据合同Sn获取最后一个结算单
     * @param contractSn 合同sn
     * @return 结算单信息
     */
    ShipSettleMainEntity findLastByContractSn(String contractSn);

    List<ContractAmount> queryContractReceiptList(@Param("map")Map<String, Object> query,@Param("filters") List<Map<String,Object>> filters);

    List<ProductShipOrderEntity> querySettleOrdersBySettleSN(String settleSN);

    List<ProductShipOrderEntity> querySettleOrdersBySnArray(String[] snArray);

    @MapKey("id")
    List<Map<String,Object>> queryExportOrdersBySettleSN(String settleSN);

    /**
     * 获取所有的设置时间
     * @return 设置信息信息
     */
    List<SetTimeVO> getSetTime();

    /**
     * 分页查询报表VO
     * @param query 查询条件
     * @return 报表信息
     */
    List<ShipSettleReportVO> queryReportPageVO(@Param("map")Map<String, Object> query);

    /**
     * 统计报表个数
     * @param map 查询条件
     * @return 执行结果
     */
    Integer queryCountReportPageVO(@Param("map") Map<String, Object> map);

    @Select("select COALESCE(SUM(currentAmount),0) from ship_settle_main WHERE state>=0")
    BigDecimal getSumCurrentAmount();

    @Select("select COALESCE(SUM(currentAmount),0) from ship_settle_main WHERE state>=0 and productCateSN=#{cateSN}")
    BigDecimal getSumCurrentAmountByCateSN(String cateSN);

    /**
     * 获取工地用户关联的结算单数量
     * @param query 查询条件
     * @param snList 合同编号集合
     * @return 查询到的数据数量
     */
    Integer queryCountByCustom(@Param("map")Map<String, Object> query,@Param("snList") List<String> snList);

    /**
     * 获取工地用户关联的结算单分页结果
     * @param query 查询条件
     * @param snList 合同编号集合
     * @return 结算单分页结果
     */
    List<ShipSettleMainEntity> queryDataByCustom(@Param("map") Map<String, Object> query,@Param("snList") List<String> snList);

    BigDecimal getSettlement();

    BigDecimal getPreSettlement();

    BigDecimal getPreSettlementCopeWith();



    List<ShipSettleMainEntity> getAll();

    List<ContractProductPriceAdjustRecordEntity> querySettleContractPPAdjust(@Param("map") Map<String, Object> searchQuery, List<Map<String, Object>> filters);

    ShipSettleMainEntity getLastSettle(String contractSN);
}
